package com.zx.eval.api.front;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.common.utils.R;
import com.zx.eval.entity.EvalQuestionEntity;
import com.zx.eval.entity.EvalResultEntity;
import com.zx.eval.entity.EvalTableEntity;
import com.zx.eval.entity.MemberEntity;
import com.zx.eval.service.EvalQuestionService;
import com.zx.eval.service.EvalResultService;
import com.zx.eval.service.EvalTableService;
import com.zx.eval.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("front/eval")
public class EvalApi {
    @Autowired
    private EvalTableService evalTableService;
    @Autowired
    private EvalQuestionService evalQuestionService;
    @Autowired
    private EvalResultService evalResultService;
    @Autowired
    private MemberService memberService;

    /**
     * 测评列表
     */
    @RequestMapping("/tableList")
    public R list() {
        QueryWrapper<EvalTableEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("category", 3);
        queryWrapper.eq("display", 1);
        queryWrapper.orderByAsc("priority");
        List<EvalTableEntity> list = evalTableService.list(queryWrapper);
        return R.ok().put("list", list);
    }

    /**
     * 测评列表
     */
    @RequestMapping("/tableOptions")
    public R tableOptions(Integer id, Long memberId) throws ParseException {
        EvalTableEntity entity = evalTableService.getCorrespondingTable(id, memberId);
        if(entity!=null){
            String options = entity.getOptions();
            String scores = entity.getScores();
            String[] optionsArray = options.split("&");
            String[] scoresArray = scores.split("&");
            if (entity.getOptionType().equals(2)) {
                QueryWrapper<EvalQuestionEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("table_id", id);
                queryWrapper.orderByAsc("question_id");
                List<EvalQuestionEntity> list = evalQuestionService.list(queryWrapper);
                EvalQuestionEntity evalQuestionEntity = list.get(0);
                optionsArray = evalQuestionEntity.getOptions().split("&");
                scoresArray = evalQuestionEntity.getScores().split("&");
            }
            return R.ok().put("optionsArray", optionsArray).put("scoresArray", scoresArray).put("info", entity);
        }
        return R.ok().put("info", entity);
    }


    /**
     * 测评问题列表
     */
    @RequestMapping("/questionList")
    public R questionList(Integer id) {
        QueryWrapper<EvalQuestionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_id", id);
        queryWrapper.orderByAsc("question_id");
        List<EvalQuestionEntity> list = evalQuestionService.list(queryWrapper);
        return R.ok().put("list", list);
    }

    /**
     * 测评问题列表
     */
    @RequestMapping("/saveResult")
    public R saveResult(@RequestBody EvalResultEntity resultEntity) {
        resultEntity.setEndTime(new Date());
        resultEntity.setOptime(new Date());
        evalResultService.save(resultEntity);
        return R.ok().put("id", resultEntity.getId());
    }

    /**
     * 测评报告
     */
    @RequestMapping("/getResult")
    public R getResult(Integer id) {
        JSONObject reportData = evalResultService.getReportByResultId(id, false);
        return R.ok().put("reportData", reportData);
    }

    /**
     * 测评报告列表
     */
    @RequestMapping("/getReportList")
    public R getReportList(Long memberId) {
        JSONArray resultList = evalResultService.getResultList(memberId);
        return R.ok().put("reportData", resultList);
    }

    /**
     * 保存用户
     * @param member
     */
    @RequestMapping("/saveMember")
    public R saveMember(@RequestBody MemberEntity member) {
        MemberEntity entity = memberService.getById(member.getId());
        entity.setBirthday(member.getBirthday());
        entity.setGender(member.getGender());
        entity.setNation(member.getNation());
        entity.setParentName(member.getParentName());
        entity.setPhone(member.getPhone());
        entity.setName(member.getName());
        memberService.updateById(entity);
        return R.ok();
    }

}
